import axios from 'axios'
import store from '@/store'

const request = axios.create({
  // baseURL: 'http://127.0.0.1:3008'
  baseURL: 'http://49.232.26.239:3008'
})
request.interceptors.request.use(
  function (config) {
    const userToken = store.state.userToken
    if (userToken) {
      config.headers.Authorization = userToken
    }

    return config
  },
  function (error) {
    return Promise.reject(error)
  }
)
// const promiseList = []
// promiseList.push(123)
request.interceptors.response.use(
  response => {
    console.log(response)
    console.log(response.config.url.includes('user'))
    if(response.data.status === 401) {
      store.commit('setUserToken', null)
      location.href = location.origin
    }
    // // 判断，如果验证码过期,这里用获取用户信息模拟 
    // if (response.config.url.includes('user')) {
    //   // 存储方法
    //   promiseList.push(
    //     request({
    //       method: response.config.method,
    //       url: response.config.url
    //     })
    //   )
    //   // 刷新token
    //   request.put('/app/v1_0/authorizations')
    // }
    // // 存储token
    // console.log(response.config.url)
    // if (response.config.url === '/app/v1_0/authorizations') {
    //   store.state.userToken = response.data.data
    //   // 发送请求
    // }
    return response
  },
  error => {
    return Promise.reject(error)
  }
)
export default request
